package system

import (
	"encoding/json"
	"sensitives/service/dic"
	"strconv"

	"github.com/gin-gonic/gin"
)

type article struct {
	commonfilter
}

var Article article

func (a *article) Check(context *gin.Context) {
	siteid, _ := strconv.Atoi(context.Param("siteid"))
	type Params struct {
		Title       string `form:"title"`
		Subtitle    string `form:"subtitle"`
		Content     string `form:"content"`
		Description string `form:"description"`
		Keyword     string `form:"keyword"`
	}
	var params Params
	err := context.Bind(&params)
	a.Sensitiveword = make([]sensitive, 0)
	type OutPut struct {
		Code bool        `json:"code"`
		Data []sensitive `json:"data"`
	}
	if err != nil {
		output := OutPut{Code: false, Data: a.Sensitiveword}
		stive_sys, _ := json.Marshal(output)
		context.Writer.Write(stive_sys)
		return
	}
	site_sys_dic := dic.CloudMap[siteid].Sys
	if params.Title != "" {
		a.filter(params.Title, "title", &site_sys_dic)
	}
	if params.Subtitle != "" {
		a.filter(params.Subtitle, "subtitle", &site_sys_dic)
	}
	if params.Content != "" {
		a.filter(params.Content, "content", &site_sys_dic)
	}
	if params.Description != "" {
		a.filter(params.Description, "description", &site_sys_dic)
	}
	if params.Keyword != "" {
		a.filter(params.Keyword, "keyword", &site_sys_dic)
	}
	output := OutPut{Code: true, Data: a.Sensitiveword}
	stive_sys, _ := json.Marshal(output)
	context.Writer.Write(stive_sys)
}
